﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

/// <summary>
/// Summary description for PaymentManager
/// </summary>
public class PaymentManager
{
    /// <summary>
    /// Link naar PIN API. Verandert niet.
    /// </summary>
    private const String apiUrl = "http://aib.aibbreda.nl/betaal.php";

	public PaymentManager()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    /// <summary>
    /// Deze methode raadpleegt de PIN API om te kijken of prijs en PIN-code van bestelling valide zijn
    /// </summary>
    /// <param name="price">Totaalprijs van bestelling in double</param>
    /// <param name="pin">Integer met pincode, 4 cijfers max.</param>
    /// <returns>True wanneer valide, InvalidPinException/false wanneer invalide</returns>
    public bool CheckPIN(double price, int pin) 
    {
        WebClient wc = new WebClient();
        StringBuilder sb = new StringBuilder(apiUrl);
        sb.Append("?bedrag=");
        string convertPrijs = price.ToString();
        string prijs = convertPrijs.Replace(",", ".");

        sb.Append(prijs);


        sb.Append("&pincode=");
        sb.Append(pin);
        Stream stream = wc.OpenRead(sb.ToString());
        StreamReader reader = new StreamReader(stream);
        String request = reader.ReadToEnd();

        if(request.Contains("SUCCES")) 
        {
            return true;
        } 
        else 
        {
            return false;
        }
    }

}